<?php

declare (strict_types=1);

namespace app\api\controller\user;

use app\api\controller\Controller;
use think\App;
use think\response\Json;
use app\api\service\user\Posts as PostsService;

/**
 * 帖子类
 * Class Goods
 * @package app\api\controller
 */
class Posts extends Controller
{

    private PostsService $postsService;
    public function __construct(App $app,PostsService $postsService)
    {
       parent::__construct($app);
       $this->postsService = $postsService;

    }

    /**帖子列表
     * @return Json
     */
    public function index():Json
    {
        $params = $this->request->get();
        $this->validate(
            $params,
            [
                'me' => 'integer',  //0 所有 1我
                'page' => 'integer',
                'limit' => 'integer'
            ]
        );
        $list = $this->postsService->getIndex($params);
        return $this->renderSuccess($list);
    }

    /**详情
     * @return Json
     */
    public function detail():Json
    {
        $params = $this->request->get();
        $this->validate(
            $params,
            [
                'id' => 'require'
            ]
        );
        $id = $params['id'];
        $info = $this->postsService->getDetail($id);
        return $this->renderSuccess($info);
    }

    /**发布
     * @return Json
     */
    public function publish():Json
    {
        $params = $this->request->post();
        $this->validate(
            $params,
            [
                'type|类型' => 'require',
                'title|标题' => 'require',
                'description|描述' => 'require',
                'relation_goods_id|关联商品' => 'integer',
                'relation_activity_id|关联活动' => 'integer'
            ]
        );
        $type = $params['type'];
        if(!in_array($type,[1,2])){
            return $this->renderError('type参数错误');
        }
        if($type == 1){
            $this->validate(
                $params,
                [
                    'image_url|图片链接' => 'require',  //多个用,分割
                ]
            );
        }else{
            $this->validate(
                $params,
                [
                    'video_url|图片链接' => 'require',  //多个用,分割
                ]
            );
        }
        $res = $this->postsService->publish($params);
        if(!$res){
            return $this->renderError('发布失败');
        }
        return $this->renderSuccess($res,'操作成功');
    }

    /**点赞、取消点赞
     * @return Json
     */
    public function like():Json
    {
        $params = $this->request->post();
        $this->validate(
            $params,
            [
                'id'  => 'require',
                'type' => 'require' // 1点赞 -1取消点赞
            ]
        );
        $res = $this->postsService->like($params);
        if(!$res){
            return $this->renderError('操作失败');
        }
        return $this->renderSuccess([],'操作成功');
    }

    //排行榜
    public function rank():Json
    {
       $access_token = $this->request->header('access-token');
       $list = $this->postsService->rankList($access_token);
       return $this->renderSuccess($list);
    }




}
